﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Web;
using Fashion.Models.Interface;

namespace Fashion.Models
{
    public class ProductCategoryRepository:IProductCategoryRepository
    {
        public DALDataContext D;

        public ProductCategoryRepository()
        {
            D = new DALDataContext();
        }

        #region Category
        
        public IEnumerable<ProductCategory> GetAllCategoryParent()
        {
            return D.ProductCategories.Where(i => i.ParentId == null);
        }

        public void AddNewCategory(ProductCategory category)
        {
            D.ProductCategories.InsertOnSubmit(category);
            D.SubmitChanges();
        }

        public ProductCategory GetCategoryById(int id)
        {
            return D.ProductCategories.FirstOrDefault(i => i.Id == id);
        }

        public void Update()
        {
            D.SubmitChanges();
        }

        public void DeleteCategory(int id)
        {
            D.ProductCategories.DeleteOnSubmit(GetCategoryById(id));
            D.SubmitChanges();
        }

        public IEnumerable<ProductCategory> GetSubCategoryByParentId(int parentId)
        {
            return D.ProductCategories.Where(i => i.ParentId == parentId);
        }

        #endregion

        #region Property

        public IEnumerable<ProductPropertyName> GetAllPropertyName()
        {
            return D.ProductPropertyNames;
        }

        public void AddNewPropertyName(ProductPropertyName propertyName)
        {
            D.ProductPropertyNames.InsertOnSubmit(propertyName);
            D.SubmitChanges();
        }

        public ProductPropertyName GetPropertyNameById(int id)
        {
            return D.ProductPropertyNames.FirstOrDefault(i => i.Id == id);
        }

        public void DeletePropertyName(int id)
        {
            D.ProductPropertyNames.DeleteOnSubmit(GetPropertyNameById(id));
            D.SubmitChanges();
        }

        #endregion

        #region ValueStatic

        public void AddNewValueStatic(ProductPropertyValueStatic valueStatic)
        {
            D.ProductPropertyValueStatics.InsertOnSubmit(valueStatic);
            D.SubmitChanges();
        }

        public void DeleteValueStatic(int id)
        {
            D.ProductPropertyValueStatics.DeleteOnSubmit(GetPropertyValueStaticById(id));
            D.SubmitChanges();
        }

        public ProductPropertyValueStatic GetPropertyValueStaticById(int id)
        {
            return D.ProductPropertyValueStatics.FirstOrDefault(i => i.Id == id);
        }

        #endregion
    }
}